package socket

import (
	"errors"
	"os"

	"golang.org/x/sys/unix"
)

// SetKeepAlivePeriod sets whether the operating system should send
// keep-alive messages on the connection and sets period between keep-alive's.
func SetKeepAlivePeriod(fd, secs int) error {
	if secs <= 0 {
		return errors.New("invalid time duration")
	}
	if err := os.NewSyscallError("setsockopt",
		unix.SetsockoptInt(
			fd,
			unix.SOL_SOCKET,
			unix.SO_KEEPALIVE,
			1)); err != nil {
		return err
	}
	switch err := os.NewSyscallError("setsockopt",
		unix.SetsockoptInt(
			fd,
			unix.IPPROTO_TCP,
			unix.TCP_KEEPINTVL,
			secs)); err {
	case nil, unix.ENOPROTOOPT: // OS X 10.7 and earlier don't support this option
	default:
		return err
	}
	return os.NewSyscallError("setsockopt",
		unix.SetsockoptInt(
			fd,
			unix.IPPROTO_TCP,
			unix.TCP_KEEPALIVE,
			secs))
}
